package cn.itsource.service.impl;

import cn.itsource.domain.Course;
import cn.itsource.mapper.CourseMapper;
import cn.itsource.query.CourseQuery;
import cn.itsource.service.ICourseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：课程Course
 */
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class CourseServiceImpl implements ICourseService {
    @Autowired
    private CourseMapper courseMapper;

    @Transactional
    @Override
    public void add(Course course) {
        courseMapper.add(course);
    }


    @Transactional
    @Override
    public void update(Course course) {
        courseMapper.update(course);
    }


    @Transactional
    @Override
    public void delete(Long id) {
        courseMapper.delete(id);
    }


    @Override
    public Course findOne(Long id) {
        return courseMapper.findOne(id);
    }


    @Override
    public List<Course> findAll() {
        return courseMapper.findAll();
    }


    @Override
    public PageInfo<Course> pageQuery(CourseQuery courseQuery) {
        //开启分页：底层要做：1.自动查询总数量total 2.自动根据驱动名实现分页查询 3.自动计算limit的第一个参数(当前页-1)*pageSize
        PageHelper.startPage(courseQuery.getCurrentPage(), courseQuery.getPageSize());
        //查询分页数据：表面上是查询所有，但你开启了分页之后就变成了分页查询
        //疑问：将query传递给mapper，mapper也没有使用啊？答：底层会根据参数实现分页查询 2.高级查询的时候要通过query传递关键字
        List<Course> courses = courseMapper.queryPage(courseQuery);
        //返回对象中有：list当前页分页数据和total总条数
        return new PageInfo<>(courses);
    }


    @Override
    public void patchDel(Long[] ids) {
        courseMapper.patchDel(ids);
    }


    @Override
    public void patchAdd(List<Course> courses) {
        courseMapper.patchAdd(courses);
    }


}
